|
|
|
|
|
|
|
The lifetime of a module variable depends on what type of module created the variable. If the variable is created in a standard module, the variable exists while the application is running. If the module is an FRM file, the variable exists while the form is in memory. The values in a module-level variable remain even if a form is unloaded and reloaded. The only way to release these module-level variables in a form is to set the form equal to the keyword Nothing. Youll learn more about Nothing in Chapter 21 The System Objects. |
|
|
|
|
|
|
|
|
To create a module variable, select either a form or a standard module in the Project Explorer window and then click the View Code button. Select (General) from the Object combo box and (Declarations) from the Procedure combo box. You may now use either the Private or Dim keyword to declare a variable. |
|
|
|
|
| Locate the (General) (Declarations) Area 1. Find the (General) (Declarations) area of the VarExamples.frm file. |  | In the (General) (Declarations) section of any module is where you create public or module-level variables. |
|
|
|
|
|
|
In the (General) (Declarations) area of all forms (standard and class modules), youll normally find the statement Option Explicit. This statement appears because in the Tools, Options menu, you checked the option Require Variable Declaration. Actually, the Require Variable Declarations check box doesnt do what it says. Instead, it should be called the Insert Option Explicit Statement check box. |
|
|
|
|
|
|
|
|
The Option Explicit statement requires the variable declarations. It tells Visual Basic to make sure all variables are explicitly declared. If the compiler finds any variables that have not been declared, it stops the compile process and informs you of the offending variable name. This option must be in every module in your application. If its missing from one module, then within that module you are free to declare variables on-the-fly, thus leading to potential bugs that will be very hard to track down. |
|
|
|
 |
|
|
|
|
TIP Always use Option Explicit and declare your variables as a specific data type. This allows the compiler to catch typos, instead of finding the bug at runtime. |
|
|
|
|
|